class AddRightsForPrinting < ActiveRecord::Migration
  
  def self.create_right(name, controller, action)
    Right.create :name => name, :controller => controller, :action => action
  end
  
  def self.up
    rights = {
      :addresses => {
        :print_customer_address => create_right('Tulosta', 'addresses', 'print_customer_address')
      }
    }
    roles = {:viewer => Role.find_by_name('Peruskäyttäjä')}
    roles[:viewer].rights << rights[:addresses][:print_customer_address]
  end
  
  def self.down
    Right.find(:all, :conditions => "action = 'print_customer_address'").each do |right|
      RoleRight.find(:all, :conditions => "right_id = #{right.id}").each do |role_right|
        role_right.destroy
      end
      right.destroy
    end
  end
end
